﻿using System.Threading.Tasks;
using Demo.Api.Data;
using Demo.Api.Models;
using Demo.Api.Services;
using Microsoft.AspNetCore.Mvc;

namespace Demo.Api.Controllers
{
    /// <summary>
    /// 事项的一个实例
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class AffairObjectController : ControllerBase
    {
        private readonly AffairObjectRepository _repository;

        public AffairObjectController(AffairObjectRepository repository)
        {
            _repository = repository;
        }
        /// <summary>
        /// 获取所有的事项实例
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Gets()
        {
            var result = await _repository.Gets2Async();
            return Ok(result);
        }
        /// <summary>
        /// 获取一个事项实例的流程图
        /// </summary>
        /// <param name="id">事项实例Id</param>
        /// <param name="flowId">关联流程的Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProcessChart(long id, long flowId)
        {
            var result = await _repository.GetProcessChart(id, flowId);
            return Ok(result);
        }
        /// <summary>
        /// 获取一个事项实例的当前可操作
        /// </summary>
        /// <param name="id">事项实例Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProcessCurrent(long id)
        {
            var result = await _repository.GetCurrentOptions(id);
            return Ok(result);
        }
        /// <summary>
        /// 获取一个事项的所有实例
        /// </summary>
        /// <param name="id">事项的Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetsByAffairId(long id)
        {
            var result = await _repository.Gets2Async(id);
            return Ok(result);
        }
        /// <summary>
        /// 获取事项实例的详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(long id)
        {
            var result = await _repository.GetAsync(id);
            return Ok(result);
        }
        /// <summary>
        /// 添加一个事项实例
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add(AffairObjectDto model)
        {
            if(model.AffairId<1)
                return BadRequest("事项Id缺失");
            if (model.FlowChartId < 1)
                return BadRequest("流程Id缺失");
            var temp = model.To();
            temp.Id = IdWorker.NextId();
            if (await _repository.Add(temp))
                return Ok(temp.Id);
            else
            {
                return BadRequest("添加失败");
            }
        }
        /// <summary>
        /// 更新事项实例信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update(long id, AffairObjectUpdateDto model)
        {
            if (id < 1)
                return await Task.FromResult(BadRequest("Id不正确"));
            model.ObjectId = id;
            if (_repository.Update(model))
                return await Task.FromResult(Ok());
            else
            {
                return await Task.FromResult(BadRequest("更新失败"));
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Delete(long id)
        {
            var result = await _repository.Delete(id);
            return Ok(result);
        }
    }
}